﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace THenu.BasicKnowledge
{
    class Program
    {
        static void Main(string[] args)
        {
            //IEnurableCollection();


            Console.WriteLine(GetDescription(LocoDirection.None));

            new XmlSerializa().SerializableTest();
        }
        static string GetDescription(Enum en)
        {
            Type type = en.GetType();
            System.Reflection.MemberInfo[] memInfo = type.GetMember(en.ToString());
            if (memInfo != null && memInfo.Length > 0)
            {
                object[] attrs = memInfo[0].GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((System.ComponentModel.DescriptionAttribute)attrs[0]).Description;
            }
            return en.ToString();
        }
        /// <summary>
        /// 
        /// </summary>
        private static void IEnurableCollection()
        {
            List<Person> list = new List<Person>();
            list.Add(new Person("MISS DD", 20, Sex.Female, LocoDirection.Left));
            list.Add(new Person("MR.  DD", 19, Sex.Male, LocoDirection.Right));
            list.Add(new Person("MRS DD", 23, Sex.Unknow, LocoDirection.None));
            list.Add(new Person("MRS DDL", 23, Sex.Unknow, LocoDirection.Right));
            list.Add(new Person("MISS DD", 20, Sex.Female, LocoDirection.Left));
            list.Add(new Person("MR.  DD", 19, Sex.Male, LocoDirection.Right));
            list.Add(new Person("MRS DD", 23, Sex.Unknow, LocoDirection.None));
            list.Add(new Person("MRS DDL", 23, Sex.Unknow, LocoDirection.Right));
            // 用以保存排序结果
            List<Person> result = new List<Person>();


            result = list.OrderBy(p => p.Sex, new ISexComparer(Sex.Male)).ToList();

            result = list.OrderBy(p => p.Direction, new ILocoDirectionComparer(LocoDirection.Left)).ToList();



            PrintPersonQueue(result);
            // 创建比较器
            ISexComparer sexComparer = new ISexComparer();
            // 将比较器作为参数带入
            result = list.OrderBy<Person, Sex>(delegate(Person p) { return p.Sex; }, sexComparer).ToList();

            result = list.OrderBy<Person, Sex>(delegate(Person p) { return p.Sex; }).ToList();
            PrintPersonQueue(result);
            // 按年龄排序 == 基础数据类型
            //result = list.OrderBy(p => p.Age).ToList();


            list.OrderBy(p => p.Sex, new ISexComparer());

            PrintPersonQueue(result);
        }

        public static void PrintPersonQueue(List<Person> list)
        {
            foreach (var item in list)
            {
                Console.WriteLine(item.ToString());
            }

            Console.ReadLine();
        }
    }
}
